﻿namespace AnalyStock.DataManage;

internal class CommDataMethod<T> where T : class, new()
{
    /// <summary>
    ///     通用数据处理泛型模型，从数据库提取数据，从在线提取数据，提取并保存数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="args">stockCode,startDate,endDate,tradeDate </param>
    /// <returns></returns>

    //获取在线数据，如果正常获取则返回，否则返回为空集合    
    internal static async Task<IList<T>> GetDataOnlineAsync(params string[] args)
    {
        try
        {
            return await CommDataHelper<T>.GetDataOnlineAsync(args).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }

        return new List<T>();
    }

    // 获取在线的数据集合，同时返回是否获取成功的标志   
    internal static async Task<(bool IsSuccees, IList<T> IList)>
        IsGetDataOnlineAsync(string ProcessName, params string[] args)
    {
        try
        {
            var result = await CommDataHelper<T>.GetDataOnlineAsync(args)
                .ConfigureAwait(false);
            if (result.IsNullOrEmpty())
            {
                throw new Exception("no data was downloaded...");
            }

            return (true, result);
        }
        catch (Exception ex)
        {
            ErrorLogPool.LogErrorMessage(ProcessName, null, args[0] ?? null, ex.Message);
        }

        return (false, null);
    }

    //根据需要判断，获取在线或在库数据
    internal static async Task<IList<T>> GetDataOnlineOrDbsetAsync(bool isOnline, params string[] args)
    {
        try
        {
            return isOnline
                ? await CommDataHelper<T>.GetDataOnlineAsync(args)
                    .ConfigureAwait(false)
                : await CommDataHelper<T>.GetDataOnDbSetAsync(args)
                    .ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }

        return new List<T>();
    }

    //异步获取数据库数据
    internal static async Task<IList<T>> GetDataOnDbSetAsync(params string[] args)
    {
        try
        {
            return await CommDataHelper<T>.GetDataOnDbSetAsync(args)
                .ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }

        return new List<T>();
    }

    // 异步下载在线数据，并且及时保存到数据库文件    
    internal static async Task DownloadToDbSetAsync(params string[] args)
    {
        try
        {
            //获取在线数据文件
            var listResult = await CommDataHelper<T>.GetDataOnlineAsync(args)
                .ConfigureAwait(false);
            //没有记录，抛出异常放弃执行
            if (listResult.IsNullOrEmpty())
            {
                throw new Exception("获取在线数据错误...!");
            }

            //保存到数据库文件
            var intResult = await CommDataHelper<T>.SaveDataToDbSetAsync(listResult, args?[0] ?? null)
                .ConfigureAwait(false);
            $"数据处理完成，在线获取数据{listResult.Count}条，更新数据库添加:{intResult}条记录！".NotifyMessage();
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }
    }

    /// <summary>
    ///     自选股保存、删除的处理方法,T类型限制为 SeleStockOfSelf
    /// </summary>
    /// <param name="args"></param>
    /// <returns></returns>
    public static async ValueTask UpdateSeleStockAsync(IList<SelectStock> list)
    {
        try
        {
            var result = await CommDataHelper<SelectStock>.SaveDataToDbSetAsync(list);
            if (list.Count > 1)
            {
                "后台数据更新完成...".NotifyMessage();
            }
            else
            {
                $"{list.FirstOrDefault().Name}:已更新完成...".NotifyMessage();
            }
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }
    }

    public static async ValueTask DelSeleStockAsync(IList<SelectStock> list)
    {
        try
        {
            var result = await CommDataHelper<SelectStock>.DeleDataOnDbSetAsync(list);
            if (result == 0)
            {
                throw new Exception("该股票不在自选股中...");
            }

            $"{list.FirstOrDefault().Name}:已从自选股中删除...".NotifyMessage();
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }
    }

    //保存概念分类
    public static async ValueTask<int> SaveConceptionClassifyAsync(IList<Conception> list, params string[] args)
    {
        try
        {
            var result = await CommDataHelper<Conception>.SaveDataToDbSetAsync(list, args[0]);
            if (result == 0)
            {
                throw new Exception("该分类已经在概念中...");
            }

            $"{args[0]}概念分类添加完成...".NotifyMessage();
            return result;
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }

        return 0;
    }
}